﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using System.Data.Linq;
namespace BAL
{
    public class TClass : BaseBAL<TClassInfo, Models.TClass>
    {
        private static string CacheName = "AllTClass";

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.TClasses.ToList(), 0, 60);
            }
        }

        /// <summary>
        /// 取得一条
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static TClassInfo GetSingleInfo(int InputId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(classInfo.Single(b => b.Id == InputId));
        }
        /// <summary>
        /// 通过名称查询对象
        /// </summary>
        /// <returns></returns>
        public static TClassInfo GetSingleInfoByName(string name)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return TransT2ToT1(classInfo.Single(b => b.Name == name));
        }
        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<TClassInfo> GetList()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return classInfo.Select(t => TransT2ToT1(t)).ToList();
        }

        public static IList<TClassInfo> GetClassInfoByGradeId(int id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return classInfo.Where(b => b.GradeId == id).Select(b => TransT2ToT1(b)).ToList();
        }

        public static IList<TClassInfo> GetClassInfoByGradeId(IList<int> ids)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return classInfo.Where(b => GetArray<int>.Get(ids).Contains(b.GradeId)).Select(b => TransT2ToT1(b)).ToList();
        }

        public static IList<int> GetClassIdByGradeId(int id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.TClass> classInfo = (IList<Models.TClass>)CacheHelper.GetCache(CacheName);
            return classInfo.Where(b => b.GradeId == id).Select(b => b.Id).ToList();
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static TClassInfo Save(TClassInfo InstInfo)
        {
            TClassInfo classInfo = InstInfo;
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
            }
            else
            {
                classInfo = Add(InstInfo);
            }
            ToCache();
            return classInfo;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Student.DelByClassId(id);
            Delete(id);
            ToCache();
        }

        public static void DelInfo(IList<int> ids)
        {
            foreach (int id in ids)
            {
                DelInfo(id);
            }
        }

        public static void DelByGradeId(int id)
        {
            IList<int> ids = GetClassIdByGradeId(id);
            DelInfo(ids);
        }
    }
}
